package org.shao.utils.venture;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

public class VentureDataProcessUtil {

    private static final String DIR = "E:\\workspace\\gitee\\navigation\\venture";

    private static final String SPLIT_CHAR = ",";

    public static void main(String[] args) throws Exception {
        VentureDataProcessUtil util = new VentureDataProcessUtil();

        List<VentureVo> ventureVos = util.read("topM-ventures-projects.json");

        List<ProjectVo> projectVos = util.parse(ventureVos);

        util.write("topM-projects-ventures.csv", projectVos);
    }

    private List<VentureVo> read(String filename) throws IOException {
        if (StringUtils.isBlank(filename)) {
            return null;
        }
        File file = Paths.get(DIR, filename).toFile();
        if (!file.exists()) {
            throw new RuntimeException("file not exists");
        }
        List<VentureVo> ventureVos = JSON.parseArray(FileUtils.readFileToString(file, "UTF-8"), VentureVo.class);
        System.out.println("ventureVos.size() = " + ventureVos.size());
        return ventureVos;
    }

    private List<ProjectVo> parse(List<VentureVo> ventureVos) {
        if (CollectionUtils.isEmpty(ventureVos)) {
            return null;
        }
        Splitter splitter = Splitter.on(SPLIT_CHAR);
        Map<String, String> projectVentureMap = Maps.newHashMap();
        for (VentureVo ventureVo : ventureVos) {
            if (ventureVo == null || StringUtils.isBlank(ventureVo.getVenture())) {
                continue;
            }
            List<String> projects = splitter.splitToList(ventureVo.getProjects());
            if (CollectionUtils.isEmpty(projects)) {
                continue;
            }
            for (String project : projects) {
                boolean containsKey = projectVentureMap.containsKey(project);
                String ventures = containsKey ? projectVentureMap.get(project) + SPLIT_CHAR + ventureVo.getVenture() : ventureVo.getVenture();
                projectVentureMap.put(project, ventures);
            }
        }
        List<ProjectVo> projectVos = Lists.newArrayList();
        Iterator<Map.Entry<String, String>> iterator = projectVentureMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            if (StringUtils.isAnyBlank(next.getKey(), next.getValue())) {
                continue;
            }
            List<String> ventures = splitter.splitToList(next.getValue());
            if (CollectionUtils.isEmpty(ventures)) {
                continue;
            }
            projectVos.add(new ProjectVo(next.getKey(), ventures.size(), next.getValue()));
        }
        System.out.println("projectVos.size() = " + projectVos.size());
        projectVos.sort((o1, o2) -> o2.getNumber().compareTo(o1.getNumber()));
        return projectVos;
    }

    private void write(String filename, List<ProjectVo> projectVos) throws IOException {
        File file = Paths.get(DIR, filename).toFile();
        if (!file.exists()) {
            boolean newFile = file.createNewFile();
            System.out.println("newFile = " + newFile);
        }
        List<String> dataList = projectVos.stream()
                .filter(Objects::nonNull)
                .map(this::writeLine)
                .collect(Collectors.toList());
        FileUtils.writeLines(file, "GBK", dataList);
        System.out.println("write success");
    }

    private String writeLine(ProjectVo projectVo) {
        if (projectVo == null) {return null;}
        StringBuilder sb = new StringBuilder();
        sb.append(projectVo.getProject());
        sb.append(SPLIT_CHAR).append(projectVo.getNumber());
        sb.append(SPLIT_CHAR).append(projectVo.getVentures());
        return sb.toString();
    }

}
